/* 
 * File:   RegularGrid.h
 * Author: walther
 *
 * Created on October 20, 2012, 8:39 PM
 */

#ifndef REGULARGRID_H
#define	REGULARGRID_H

#include "Object.h"
#include "Grid.h"
#include "GridIterator.h"


class RegularGrid : public Grid
{
public:
    /**
     * Constructor
     * @param objects Array with the objects from the scene
     * @param nX Number of cells along the X axis
     * @param nY Number of cells along the Y axis
     * @param nZ Number of cells along the Z axis
     */
    RegularGrid( std::vector<Object*>& objects, int nX, int nY, int nZ );
    
    /**
     * Destructor
     */
    virtual ~RegularGrid();
    
    /**
     * From Grid:
     * Creates an iterator to "walk" through the grid
     * @param ray Ray that will guide the iteration
     * @return Pointer to the built iterator
     */
    GridIterator* getIterator( Ray& ray );
    
    /**
     * @param x Cell x coordinate
     * @param y Cell y coordinate
     * @param z Cell z coordinate
     * @return Pointer to the cell in the received position
     */
    GridCell* getCell( int x, int y, int z );

    /**
     * Returns the cell index given a point in space. If x, y or z are negative,
     * the point is not in the grid
     * @param point Point world coordinates
     * @param x Cell x coordinate
     * @param y Cell y coordinate
     * @param z Cell z coordinate
     */
    void getCellIndex( Vector4D point, int& x, int& y, int& z );
    
    /**
     * @return Size of the cell in each direction
     */
    Vector4D getCellSizes();
    
    /**
     * Tells the number of cells in each direction
     * @param nx Number of cells in x
     * @param ny Number of cells in y
     * @param nz Number of cells in z
     */
    void getCellNumber( int& nx, int& ny, int& nz );
    
private:
    /**
     * Builds a regular grid from the array of objects by filling the _cells array.
     * @param objects Objects in the scene
     */
    void buildGrid( std::vector<Object*>& objects );
    
    /** size of the cell in each direction */
    Vector4D _cellSizes;
    
    /** Number of cells  in each direction 0=x, 1=y and 2=z */
    int _cellNumber[3];
};

#endif	/* REGULARGRID_H */

